//
// Created by KangLe on 2017/8/23.
//

#include "com_kangle_changergb_ChangeRgbUtil.h"
#include "MyLog.h"


int RGB(int r,int g,int b)
{
    return 0xff <<24 | r << 16 | g << 8 | b;
//    return r +  g + b ;
}
JNIEXPORT void  Java_com_kangle_changergb_ChangeRgbUtil_changeRGB2RGB888
        (JNIEnv *env, jclass obj, jbyteArray jBuffer, jintArray out,jint length){

    jbyte* image =  env->GetByteArrayElements(jBuffer, 0);
//    if (image == NULL){
//        LOGD("image == NULL");
//        return ;
//    }
//    jint *carr = NULL;
    jint *carr  = env->GetIntArrayElements(out, 0);
//    if(carr == NULL) {
//        return ;
//    }
    for (int i = 0; i < length; i++) {
        int r = 0;
        int g = 0;
        int b = 0;
        r = (int)image[3 * i];
        g = (int)image[3 * i + 1];
        b = (int)image[3 * i + 2];
        if (r < 0) r += 255;
        if (g < 0) g += 255;
        if (b < 0) b += 255;
        carr[i] = RGB(r, g, b);
    }
    env->ReleaseByteArrayElements(jBuffer, image, 0);
    env->ReleaseIntArrayElements(out, carr, 0);
}

